package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.Student;
import com.wuseguang.springcloud.service.StudentService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author 
 * @since 2019-12-29
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StudentService studentServiceImpl;

    /**
     * 查询学校学生
     * @param request
     * @param classId 班级id
     * @param stuName 学生名字
     * @return
     */
    @RequestMapping("/findAllStudent")
    public Result findAllStudent(RequestParameterWrapper request,
                                 @RequestParam(value = "classId",required = false)Integer classId,
                                 @RequestParam(value = "stuName",required = false)String stuName,
                                 @RequestParam(value = "phone",required = false)String  phone,
                                 @RequestParam(value = "indexNo",required = false,defaultValue = "1")Integer indexNo,
                                 @RequestParam(value = "rows",required = false,defaultValue = "20")Integer rows){

        logger.info("查询学校学生 StudentController -> findAllStudent" );

        //学校id
        String schoolId = request.getParam("schoolId");

        String teacherId = request.getParam("teacherId");

        //管理员
        String admin = request.getParam("admin");

        return studentServiceImpl.findAllStudent(classId,stuName,schoolId,indexNo,rows,teacherId,admin,phone);
    }

    /**
     * 删除学生信息
     * @param request
     * @param id 学生id
     * @return
     */
    @RequestMapping("/deleteStudent")
    public Result deleteStudent(RequestParameterWrapper request,
                                @RequestParam(value = "id",required = false)Integer id){
        logger.info("删除学生信息 StudentController -> deleteStudent");
        return studentServiceImpl.deleteStudent(id);
    }

    /**
     * 添加学生信息
     * @param request
     * @param student 学生信息
     * @return
     */
    @RequestMapping("/addStudent")
    public Result addStudent(RequestParameterWrapper request,@RequestBody Student student){
        logger.info("添加学生信息 StudentController -> addStudent");
        //学校id
        String schoolId = request.getParam("schoolId");
        student.setSchoolId(Integer.valueOf(schoolId));
        return studentServiceImpl.addStudent(student);
    }


    /**
     * 批量添加学生信息
     * @param request
     * @param student
     * @return
     */
    @RequestMapping("/batchAdditionStudent")
    public Result batchAdditionStudent(RequestParameterWrapper request,@RequestParam(value = "student",required = false) String  student){
        logger.info("批量添加学生信息 StudentController -> batchAdditionStudent");
        //获取学校id
        String schoolId = request.getParam("schoolId");
        //判断是否为空
        if(StringUtils.isEmpty(student)){
            return  new Result(CodeUtil.NO_STUDENT_DATA);
        }

       /* //获取管理员状态
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以删除教师信息");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        return studentServiceImpl.batchAdditionStudent(schoolId,student);

    }

    /**
     * 重置全校学生的密码
     * @param request
     * @return
     */
    @RequestMapping("/resetAllStudentPwd")
    public Result resetAllStudentPwd(RequestParameterWrapper request,
                                     @RequestParam(value = "onePassWord",required = false)String onePassWord,
                                     @RequestParam(value = "twoPassWord",required = false)String twoPassWord){
        logger.info("重置全校学生的密码 StudentController -> resetAllStudentPwd");
        //判断密码是否为空
        if(StringUtils.isEmpty(onePassWord)||StringUtils.isEmpty(twoPassWord)){
            logger.info("修改的密码为空，不执行修改。");
            return new Result(CodeUtil.THE_PASSWORD_IS_EMPTY);
        }
        //判断是否相同
        if(!onePassWord.equals(twoPassWord)){
            return new Result(CodeUtil.PASSWORDS_ARE_INCONSISTENT);
        }
        //获取学校id
        String schoolId = request.getParam("school_id");
        return studentServiceImpl.resetAllStudentPwd(schoolId,onePassWord);
    }

    /**
     * 重置学生密码
     * @param request
     * @param id 学生id
     * @return
     */
    @RequestMapping("/resetStudentPwd")
    public Result resetStudentPwd(RequestParameterWrapper request,
                                  @RequestParam(value = "id",required = false)Integer id,
                                  @RequestParam(value = "onePassWord",required = false)String onePassWord,
                                  @RequestParam(value = "twoPassWord",required = false)String twoPassWord){
        logger.info("重置学生的密码 StudentController -> resetStudentPwd");
        /*//获取管理员状态
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员不可以删除教师信息");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        //判断密码是否为空
        if(StringUtils.isEmpty(onePassWord)||StringUtils.isEmpty(twoPassWord)){
            logger.info("修改的密码为空，不执行修改。");
            return new Result(CodeUtil.THE_PASSWORD_IS_EMPTY);
        }
        //判断是否相同
        if(!onePassWord.equals(twoPassWord)){
            return new Result(CodeUtil.PASSWORDS_ARE_INCONSISTENT);
        }

        return studentServiceImpl.resetStudentPwd(id,twoPassWord);
    }

    /**
     * 修改学生信息
     * @param request
     * @param student
     * @return
     */
    @RequestMapping("/updateStudent")
    public Result updateStudent(RequestParameterWrapper request,@RequestBody Student student){
        logger.info("添加学生信息 StudentController -> updateStudent");
        //学校id
        String schoolId = request.getParam("schoolId");
        student.setSchoolId(Integer.valueOf(schoolId));
        return  studentServiceImpl.updateStudent(student);
    }

    /**
     * 检测学生数据
     * @param request
     * @param student 学生数据
     * @return
     */
    @RequestMapping("/testStudentData")
    public Result testStudentData(RequestParameterWrapper request,
                                  @RequestParam(value = "student",required = false) String  student){
        logger.info("检测学生数据是否正确");
        //判断是否为空
        if(StringUtils.isEmpty(student)){
            return  new Result(CodeUtil.NO_STUDENT_DATA);
        }
        //获取学校id
        String schoolId = request.getParam("schoolId");

        return  studentServiceImpl.testStudentData(schoolId,student);
    }

    /**
     * 导出学生模板
     * @param request
     * @param response
     */
    @RequestMapping("/exportStudentTemplate")
    public void exportStudentTemplate(RequestParameterWrapper request,

                                        HttpServletResponse response){
        logger.info("导出学生模板");
        studentServiceImpl.exportStudentTemplate(request,response);
    }

    /**
     * 导出学生模板
     * @param request
     * @param response
     */
    @RequestMapping("/exportStudent")
    public void exportStudent(RequestParameterWrapper request,HttpServletResponse response,@RequestParam(value = "classId") Integer classId ){
        logger.info("导出学生信息");
        studentServiceImpl.exportStudent(request,response,classId);
    }

    /**
     * 查询有数据权限的学生
     * @param request
     * @return
     */
    @RequestMapping("/findJurisdictionStudent")
    public Result findJurisdictionStudent(RequestParameterWrapper request){
        logger.info("查询学校有数据权限的学生");
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取学校id
        String schoolId = request.getParam("schoolId");
        return studentServiceImpl.findJurisdictionStudent(teacherId,schoolId);
    }

    /**
     * 手动同步学生数据 (海威达)
     * @param request
     * @return
     */
    @RequestMapping("/manualSynchronization")
    public Result manualSynchronization(RequestParameterWrapper request){
        //获取学校id
        String schoolId = request.getParam("schoolId");
        return studentServiceImpl.manualSynchronization(schoolId);
    }

    /**
     * 手动同步学生人脸数据 (海威达)
     * @param request
     * @return
     */
    @RequestMapping("/manualFaceSynchronization")
    public Result ManualFaceSynchronization(RequestParameterWrapper request){
        //获取学校id
        String schoolId = request.getParam("schoolId");
        return studentServiceImpl.ManualFaceSynchronization(schoolId);
    }
}

